package br.com.scpe.ejb.sessionBean;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.scpe.ejb.entity.Funcionario;
import br.com.scpe.ejb.entity.Ponto;

@Stateless
public class PontoSessionBean {
	
	@PersistenceContext
	private EntityManager entityManager;
	
	public void gerarEntrada(Ponto ponto) {
		
		Funcionario funcionario = entityManager.find(Funcionario.class, ponto.getFuncionario().getIdFuncionario());
		ponto.setFuncionario(funcionario);
		this.entityManager.persist(ponto);
	}
	
	public void gerarIntervalo (Ponto ponto){
		this.entityManager.merge(ponto);		
	}
	
	public void gerarSaida(Ponto ponto) {
		
		Ponto pontoBuscasdo = entityManager.find(Ponto.class, ponto.getIdPonto());
		pontoBuscasdo.setHoraSaida(ponto.getHoraSaida());
		
		if(pontoBuscasdo != null){
			this.entityManager.merge(pontoBuscasdo);
		}
	}
	
	public Date consultarHoraEntrada(Long idFuncionario){
		
		Query query = entityManager.createNativeQuery("SELECT HORAENTRADA FROM PONTO where fk_idFuncionario = ? and CURRENT_DATE = cast(HORAENTRADA as date)");
		query.setParameter(1, idFuncionario);
		
		try {
			return (Date) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	public Date consultarHoraIntervalo(Long idFuncionario){
		
		Query query = entityManager.createNativeQuery("SELECT HORAINTERVALO FROM PONTO where fk_idFuncionario = ? and CURRENT_DATE = cast(HORAENTRADA as date)");
		query.setParameter(1, idFuncionario);
		
		try {
			return (Date) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	public Date consultarHoraSaida(Long idFuncionario){
		
		Query query = entityManager.createNativeQuery("SELECT HORASAIDA FROM PONTO where fk_idFuncionario = ? and CURRENT_DATE = cast(HORASAIDA as date)");
		query.setParameter(1, idFuncionario);
		
		try {
			return (Date) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Ponto> consultarTodosPontosDoFuncionario(Long idFuncionario){
		
		Query query = entityManager.createNamedQuery("Ponto.consultarTodosPontoDoFuncionario");
		query.setParameter("idFuncionario", idFuncionario);
		
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	public Long consultarIdPontoDia(Long idFuncionario){
		
		Query query = entityManager.createNativeQuery("SELECT IDPONTO FROM PONTO where fk_idFuncionario = ? and CURRENT_DATE = cast(HORAENTRADA as date)");
		query.setParameter(1 , idFuncionario);
		try {
			BigInteger result = (BigInteger) query.getSingleResult();
			return result.longValue();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	public Ponto consultaPorId(Ponto ponto) {		
		return entityManager.find(Ponto.class, ponto.getIdPonto());
	}
	
	@SuppressWarnings("unchecked")
	public List<Ponto> consultaTodosPontos(){
		Query query = entityManager.createNamedQuery("Ponto.consultarTodosPontos");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}	
	
	public Ponto consultaPontoFuncionario(Long idPonto){
		Query query = entityManager.createNamedQuery("Ponto.consultarPontoFuncionario");
		query.setParameter("idPonto", idPonto);
		try {
			return (Ponto) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}	
	
	@SuppressWarnings("unchecked")
	public List<Ponto> consultarTodosPontosDoFuncionarioPorMesAno(Long idFuncionario, Long mes, Long ano){
		Query query = entityManager.createQuery("select p From Ponto p where p.funcionario.idFuncionario = :idFuncionario and MONTH(p.horaEntrada) = :monthEntrada and year(p.horaEntrada) = :yearEntrada");
		query.setParameter("idFuncionario", idFuncionario);
		query.setParameter("monthEntrada", Integer.parseInt(String.valueOf(mes)));
		query.setParameter("yearEntrada", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Ponto> consultarTodosPontosDoFuncionarioPorMes(Long idFuncionario, Long mes){
		Query query = entityManager.createQuery("select p From Ponto p where p.funcionario.idFuncionario = :idFuncionario and MONTH(p.horaEntrada) = :monthEntrada");
		query.setParameter("idFuncionario", idFuncionario);
		query.setParameter("monthEntrada", Integer.parseInt(String.valueOf(mes)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Ponto> consultarTodosPontosDoFuncionarioPorAno(Long idFuncionario, Long ano){
		Query query = entityManager.createQuery("select p From Ponto p where p.funcionario.idFuncionario = :idFuncionario and  year(p.horaEntrada) = :yearEntrada");
		query.setParameter("idFuncionario", idFuncionario);
		query.setParameter("yearEntrada", Integer.parseInt(String.valueOf(ano)));
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Ponto> consultarPontoFuncionarioProjeto(Long idFuncionario, Long idProjeto){
		Query query = entityManager.createNamedQuery("Ponto.consultarPontoFuncionarioProjeto");
		query.setParameter("idFuncionario", idFuncionario);
		query.setParameter("idProjeto", idProjeto);
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
}